'/
' ProjectName
' 
' =========================================================================
' 
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU General Public License
' along with this program.  If not, see <http://www.gnu.org/licenses/>.
'/

type SoundQue extends Base

	'// -------------------------------------------------------------------------------------------------------------
	'// Create, setup and return a sound que.
	'// -------------------------------------------------------------------------------------------------------------
	function create:SoundQue(name:string)
	
		local que:SoundQue = new SoundQue;
		que.m_name = name;
		return que;
	
	end function
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Updates the que. When this method is called, the first sound in the que gets played. Only the last sound in
	'// the que gets to be played all the way through to the end.
	'// -------------------------------------------------------------------------------------------------------------
	method update()
	
		local snd:Sound = Sound(m_soundList.first());
		local last:Sound = Sound(m_soundList.last());
		
		if (not snd) ..
			return;
		
		if (not m_curSoundPlaying) ..
			m_curSoundPlaying = snd;
		
		if (m_curSoundPlaying = last and snd.getPosition() >= snd.m_length) then
		
			snd.stop();
			m_curSoundPlaying = null;
		
		else
		
			snd.play();
			m_curSoundPlaying = snd;
			m_soundList.firstLink().remove();
		
		end if
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Add a sound to the que. Distance = delta pos between two actors.
	'// -------------------------------------------------------------------------------------------------------------
	method addSound(snd:Sound, distance:double, panning:int = 0)
	
		if (distance > 3) then
		
			snd.setGain(1.0 / (distance - 3));
		
		end if
		
		m_soundList.addLast(snd);
	
	end method
	
	'// -------------------------------------------------------------------------------------------------------------
	'// The name of the que.
	'// -------------------------------------------------------------------------------------------------------------
	field m_name:string;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// The sounds that are put in the que.
	'// -------------------------------------------------------------------------------------------------------------
	field m_soundList:TList = new TList;
	
	'// -------------------------------------------------------------------------------------------------------------
	'// Currently playing sound.
	'// -------------------------------------------------------------------------------------------------------------
	field m_curSoundPlaying:Sound;

end type
